<template>
  <div>
    <el-card class="box-card" style="margin:30px">
      <div slot="header" class="clearfix">
        <el-button size="small" type="primary" @click="add">新增角色</el-button>
      </div>
      <el-table
        :data="list"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column
          label="#"
          type="index"
          width="50"
          align="center"
        />
        <el-table-column
          prop="roleName"
          label="角色名称"
          width="200"
        />
        <el-table-column
          prop="roleCode"
          label="角色编码"
          width="200"
        />
        <el-table-column
          prop="remark"
          label="备注"
          width="300"
        />
        <el-table-column
          prop="createTime"
          label="添加时间"
          width="200"
        />
        <el-table-column
          label="操作"
          width="300"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="success" size="mini" @click="edit(scope.row.id)">编辑</el-button>
            <el-button size="mini" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="bottom">
        <el-pagination
          :current-page="page.currentPage"
          :page-sizes="page.pageSizes"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <Detail ref="details" />
  </div>
</template>
<script>
import { findRolesByPage, removeRole } from '@/api/auth/role'
import mixin from '@/mixins/index'
import Detail from './components/detail'
export default {
  components: {
    Detail
  },
  mixins: [mixin],
  data() {
    return {
      list: []
    }
  },
  computed: {
  },
  created() {
    this.getRoleList()
  },
  methods: {
    getRoleList() {
      findRolesByPage(1, 10).then(res => {
        this.list = res.data.rows
        this.page.total = res.data.total
      })
    },
    handleSizeChange(val) {
      this.page.size = val
      this.getRoleList()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getRoleList()
    },
    add() {
      this.$refs.details.open()
    },
    edit(id) {
      this.$refs.details.open(id)
    },
    del(id) {
      removeRole(id).then(res => {
        if (res.success) {
          this.$message.success('删除成功')
        } else {
          this.$message.error('删除失败')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .bottom{
    display: flex;
    justify-content: flex-end;
    margin: 20px 5px;
  }
</style>
